<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="utf-8">
</head>
<body>

<button id="set">设置cookie</button>
<button id="remove">删除cookie</button>
<script type="text/javascript">

onload = function () {
	var cookieName = getCookie('age');
	console.log(cookieName)
	// console.log(document.cookie)
}

remove.onclick = function(){
	removeCookie('age')
}

set.onclick = function(){	
	setCookie('name','小王',7);
	setCookie('height','180',7)
}

function getCookie(name){
	var arr = document.cookie.split('; ');//以；分开
	
	for(let i=0;i<arr.length;i++){
		var item = arr[i].split('=')//遍历数组加=号，然后提出内容
		// console.log(item)
		if(name == item[0]){
			return item[1]
		}
	}

		// return '';
}

function setCookie(name, value, day) {
	var oDate = new Date(); // 当前时间
	// console.log(oDate.getDate() + day)
	oDate.setDate(oDate.getDate() + day)
	//setDate 方法 获取第n天之后的日期
	document.cookie = `${name}=${value}; expires=${oDate}`;
}

function removeCookie(name){
	setCookie(name, 123, -1)
	//cookie过期  浏览器去判断 当前的expires这个时间有没有在今天之前
}





</script>
</body>
</html>